﻿using B.MES.Customer.Application.IApplication;
using B.MES.Customer.Domain;
using B.MES.Customer.Domain.DTO;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;

namespace B.MES.Customer.Read.WebApi.Controllers
{
    /// <summary>
    /// 产品管理
    /// </summary>
    [Route("api/[controller]/[action]")]
    [ApiController]
    public class ProductController : ControllerBase
    {
        private readonly IProductApplication _productApplication;

        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="productApplication"></param>
        public ProductController(IProductApplication productApplication)
        {
            _productApplication = productApplication;
        }

        /// <summary>
        /// 绑定产品属性表的下拉框
        /// </summary>
        /// <returns>返回产品属性表数据</returns>
        [HttpGet]
        public IActionResult GetProductShow()
        {
            return Ok(_productApplication.GetProductShow());
        }
        /// <summary>
        /// 绑定产品类别表的下拉框
        /// </summary>
        /// <returns>返回产品类别表数据</returns>
        [HttpGet]
        public IActionResult GetProductinfoShow()
        {
            return Ok(_productApplication.GetProductinfoShow());
        }
        /// <summary>
        /// 绑定产品单位表的下拉框
        /// </summary>
        /// <returns>返回产品单位表数据</returns>
        [HttpGet]
        public IActionResult GetProductUnitInfoShow()
        {
            return Ok(_productApplication.GetProductUnitInfoShow());
        }

        /// <summary>
        /// 添加产品
        /// </summary>
        /// <param name="productDTO"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult AddProduct(ProductDTO productDTO)
        {
            try
            {
                return  Ok(_productApplication.AddProduct(productDTO));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 文件上传
        /// </summary>
        /// <param name="file"></param>
        /// <returns></returns>
        [HttpPost]
        public IActionResult UpFile(IFormFile file)
        {
            string path = Directory.GetCurrentDirectory() + "/wwwroot/PIC/"  + file.FileName;
            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                file.CopyTo(fs);
            }

            return Ok(file.FileName);
        }

        /// <summary>
        /// 显示产品列表
        /// </summary>
        /// <param name="product_Name">产品名称</param>
        /// <param name="model_Number">规格型号</param>
        /// <param name="brand">品牌</param>
        /// <param name="page">页码</param>
        /// <param name="size">页容量</param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetUserListByKeyword(string? product_Name, string? model_Number, string? brand, int page, int size)
        {
            try
            {
                return Ok(_productApplication.GetUserListByKeyword(product_Name, model_Number, brand, page, size));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 删除产品信息
        /// </summary>
        /// <param name="product_Id"></param>
        /// <returns></returns>
        [HttpDelete]
        public IActionResult DeleteData(int product_Id)
        {
            try
            {
                return Ok(_productApplication.DeleteData(product_Id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 产品反填
        /// </summary>
        /// <param name="product_Id"></param>
        /// <returns></returns>
        [HttpGet]
        public IActionResult GetStockInSel(int product_Id)
        {
            try
            {
                return Ok(_productApplication.GetStockInSel(product_Id));
            }
            catch (Exception)
            {

                throw;
            }
        }

        /// <summary>
        /// 产品修改
        /// </summary>
        /// <param name="productDTO"></param>
        /// <returns></returns>
        [HttpPut]
        public IActionResult UpdateProductSel(ProductDTO productDTO)
        {
            try
            {
                return Ok(_productApplication.UpdateProductSel(productDTO));
            }
            catch (Exception)
            {

                throw;
            }
        }
    }
}
